<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>用户管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPageByCondition()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系方式" align="center"></el-table-column>
                <el-table-column prop="station" label="用户状态" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs type="card">
                            <el-tab-pane label="基本信息">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名称" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="password">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="联系电话" prop="keyword">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="是否启用">
                                                <el-tooltip :content="'Switch value: ' + flagvalue" placement="top">
                                                    <el-switch
                                                            v-model="formData.station"
                                                            default=true>
                                                    </el-switch>
                                                </el-tooltip>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="用户角色">
                                                <table class="datatable">
                                                    <tbody>
                                                    <tr v-for="c in tableData">
                                                        <td>
                                                            <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                                        </td>
                                                        <td><label :for="c.name">{{c.name}}</label></td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="handleAddCancel()">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="edit-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs type="card">
                            <el-tab-pane label="基本信息">
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名称" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="联系电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="是否启用">
                                                <el-tooltip :content="'Switch value: ' + flagvalue" placement="top">
                                                    <el-switch
                                                            v-model="formData.station"
                                                            default=true>
                                                    </el-switch>
                                                </el-tooltip>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="用户角色">
                                                <table class="datatable">
                                                    <tbody>
                                                    <tr v-for="c in tableData">
                                                        <td>
                                                            <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                                        </td>
                                                        <td><label :for="c.name">{{c.name}}</label></td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="handleEditCancel()">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            roleIds: [],
            flagvalue:true,
            tableData: [],
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                username: [{required: true, message: '用户名称为必填项', trigger: 'blur'}],
                password: [{required: true, message: '密码称为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();//VUE对象初始化完成后执行分页
        },
        methods: {
            //条件查询分页
            findPageByCondition(){
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //分页查询
            findPage() {
                //发送AJax请求
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findPage.do", param).then((res) => {
                    this.pagination.total = res.data.total;
                    for (var i = 0; i< res.data.rows.length;i++){
                        if(res.data.rows[i]['station'] == true){
                            res.data.rows[i]["station"] = "启用";
                        }else{
                            res.data.rows[i]["station"] = "禁用";
                        }
                    }
                    this.dataList = res.data.rows;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置新的当前页码
                this.pagination.currentPage = currentPage;
                //重新调用findPage进行查询
                this.findPage();
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 关闭  新建 弹出框
            handleAddCancel() {
                this.dialogFormVisible = false;
            },
            // 关闭编辑弹出框
            handleEditCancel() {
                this.dialogFormVisible4Edit = false;
            },
            handleCreate(){
                //弹出新建窗口
                this.dialogFormVisible = true;
                //重置表单
                this.resetForm();
                //默认切换到第一个标签页（基本信息）
                //this.activeName = 'first';
                //重置
                this.roleIds = [];

                //发送ajax请求查询所有检查项
                axios.get("/role/findAll.do").then((res) => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },
            //添加
            handleAdd() {
                //  新增窗口表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    //校验成功
                    if (valid) {
                        // 发送Ajax请求，进行添加
                        axios.post("/user/add.do?roleIds=" + this.roleIds,
                            this.formData).then((res) => {
                            if (res.data.flag) {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                //关闭弹框
                                this.dialogFormVisible = false;
                                //勾选的权限重置
                                this.roleIds = [];
                                //查询列表
                                this.findPage();
                            } else {
                                this.$message.error(res.data.message + "[" + res.data.data + "]");
                            }
                        }).catch((r)=>{
                            this.showMessage(r);
                        });
                        //校验失败
                    } else {
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                axios.get("/user/findById.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        this.dialogFormVisible4Edit = true;
                        //默认切换到第一个标签页（基本信息
                        this.formData = res.data.data;
                        axios.get("/role/findAll.do").then((res) => {
                            if (res.data.flag) {
                                this.tableData = res.data.data;
                                axios.get("/user/findRoleIdsByUserId.do?id=" + row.id).then((res) => {
                                    if (res.data.flag) {
                                        this.roleIds = res.data.data;
                                    } else {
                                        this.$message.error(res.data.message);
                                    }
                                });
                            } else {
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        this.$message.error("获取数据失败，请刷新当前界面！")
                    }
                });
            },
            //编辑
            handleEdit() {
                // 编辑窗口表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    //校验成功
                    if (valid) {
                        //发送Ajax请求
                        axios.post("/user/edit.do?roleIds=" + this.roleIds,
                            this.formData).then((res) => {
                            if (res.data.flag) {
                                this.dialogFormVisible4Edit = false;
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(res.data.message + "[" + res.data.data + "]");
                            }
                        }).catch((r)=>{
                            this.showMessage(r);
                        }).finally(() => {
                            this.findPage();
                        });
                    } else {
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });

            }
        }
    })
</script>
</html>